Online Type-Directed Partial Evaluation

نویسنده

  • Olivier Danvy
چکیده

syntax rewriting (macros): The idea is as old as programming languages. Macros were fraught with name-capture peril until the advent of hygienic macro-expansion [30]. Our domain-speci c language over residual abstract syntax follows the lead of Chez Scheme's syntactic extensions [24], namely, it uses pattern matching, fenders (i.e., guards), and with-expressions. Let insertion: Mogensen suggested to insert residual let expressions to handle partially static values [31]. Bondorf and Danvy put let insertion at the core of the Similix partial evaluator to handle call unfolding in the presence of dynamic actual parameters [10].5 Let insertion is a cornerstone of call-by-value type-directed partial evaluation [18]. Two-level -expansion: The idea has been used both in the CPS transformation and in partial evaluation [19, 20]. Partial evaluation: With two exceptions, all other partial evaluators operate over the text of their source programs [14, 28]. The rst exception is Berlin's [6, 7]. As described above, it operates by running the source program with instrumented primitive operators. The second exception is \generating extensions," which means \dedicated specializers as obtained by self-application" in the partial-evaluation jargon [14, 28]. In contrast to a general-purpose specializer, a generating extension incurs no interpretive overhead. In our experience, though, at least with Similix, generating extensions are still less e cient than type-directed partial evaluation in practice [9, 21, 34]. Type specialization: Hughes has recently presented a radically new way of looking at and implementing partial evaluation for functional programs [27]. This new approach departs from traditional partial evaluation in that the partial evaluator does not proceed by symbolic interpretation. Instead, it piggy-backs on type inference, as directed by the control and data ows of the source program, and thus does not follow the same steps as a traditional partial evaluator. Type specialization di ers from type-directed partial evaluation in that it still operates on the text of source programs, 5\Ensuring that dynamic side-e ects do not disappear and are not duplicated, and keeping them in the same order as in the source program" is one of the oldest mantras of Similix. Type-directed partial evaluation shares the same mantra. 24 albeit in an unspeci ed order. In contrast, a type-directed partial evaluator follows the same steps as a traditional partial evaluator performing symbolic interpretation and in the same order | it just does so without interpretive overhead. Other implementations of type-directed partial evaluation: We distinguish between meta-level and native implementations. A meta-level implementation consists of an interpreter enriched with two-level -expansion. Altenkirch, Hofmann, and Streicher [1, 2] and Sheard [35] have written such interpreters, the former for pure call by name and the latter for applied call by value. Both also handle polymorphism, and in addition, Sheard's treats inductive data types through a xed-point operator and \lazy re ection." Lazy re ection amounts to delaying -expansion for contravariant types, which is possible in a meta-level implementation. Otherwise, Sheard's implementation is online: it o ers pure primitive operators that probe their operands with the usual xed policy. (We also considered that approach in Section 4.5 of our earlier work [17].) A native implementation directly processes compiled code. Berger and Schwichtenberg have such an implementation, in Scheme [4, 5]. Filinski does too, in Standard ML,6 and so do Zhe Yang, also in Standard ML,7 and Rhiger, in Gofer [34]. In the summer of 1997, Balat and Danvy have combined a native implementation with run-time code generation, in Caml [3]. An ML native implementation cannot o er probing primitive operators since they are fundamentally overloaded. Our (o ine) Scheme implementation also handles polymorphism. According to published numbers for comparable examples (including inductive data types), native implementations perform between 1000 and 10000 times faster than meta-level implementations. The author's earlier implementations have already been used at other institutions [23, 26]. The present online implementation so far is only used by the author and his students, e.g., for Action Semantics [21, 32, 34]. We nd it more exible and about as e cient, despite the extra processing activity of primitive operators. 6Personal communication, spring 1995. 7Personal communication, spring 1996. 25 5 Conclusion and IssuesBecause o ine type-directed partial evaluation only handles closed terms,it requires the user to close every source program by lambda-abstracting allits free variables, which is awkward in practice. We have extended our type-directed partial evaluator with typed primitive operations ( -rules), whosedefault policy is to proceed if all the operands are static and to residualizethe operation otherwise. The user can specify the partial-evaluation policyof an operator in two ways: (1) by specifying a lter deciding whether toperform the operation or to residualize it; and (2) by specifying how to resid-ualize the operation. This extension makes type-directed partial evaluationmore modular (the user can write or use libraries of primitive operators)and more exible (the partial-evaluation behaviour of primitive operatorsis context-sensitive, i.e., their binding times are polyvariant). Online type-directed partial evaluation is also naturally incremental in that while residualprograms can be compiled and run, they can also be compiled and furtherspecialized should the opportunity arise.Contribution: Foremost, we report an online extension of a native im-plementation of type-directed partial evaluation for Scheme. This extensionhandles both pure and impure primitive operators, and both their unfold-ing and residualization strategies can be speci ed by the user. The formeris achieved with lters and the latter through a domain-speci c languagefor residual terms, designed jointly with Morten Rhiger [34]. The resultingimplementation meshes smoothly with our earlier implementation of type-directed partial evaluation. It is available from the author on request.8 Inthis article, we also have attempted to provide a comprehensive practicaloverview of type-directed partial evaluation.Limitations: They are three-fold. Practical: the specialization strategyof type-directed partial evaluation is monovariant [14, 28], and the use of ourtype-directed partial evaluator does require some skill, since specializationcan diverge or yield huge residual programs. Fundamental: only the call-by-name version of type-directed partial evaluation has been formalized. Andconceptual: inductive types are still out of reach in all their generality.8http://www.brics.dk/~danvy26 AcknowledgementsGrateful thanks to Morten Rhiger for many pleasant discussions aboutcase-syntax, and to Julia Lawall and Karoline Malmkj r for valuable andtimely comments on an earlier version of this article. Thanks are also dueto the anonymous reviewers.References[1] Thorsten Altenkirch, Martin Hofmann, and Thomas Streicher. Categor-ical reconstruction of a reduction-free normalization proof. In David H.Pitt and David E. Rydeheard, editors, Category Theory and ComputerScience, number 953 in Lecture Notes in Computer Science, pages 182{199. Springer-Verlag, 1995.[2] Thorsten Altenkirch, Martin Hofmann, and Thomas Streicher. Reduct-ion-free normalisation for a polymorphic system. In Proceedings of theEleventh Annual IEEE Symposium on Logic in Computer Science, NewBrunswick, New Jersey, July 1996. IEEE Computer Society Press.[3] Vincent Balat and Olivier Danvy. Strong normalization by run-timecode generation. Technical Report BRICS RS-97-43, Department ofComputer Science, University of Aarhus, Aarhus, Denmark, December1997.[4] Ulrich Berger. Program extraction from normalization proofs. InM. Bezem and J. F. Groote, editors, Typed Lambda Calculi and Ap-plications, number 664 in Lecture Notes in Computer Science, pages91{106, Utrecht, The Netherlands, March 1993.[5] Ulrich Berger and Helmut Schwichtenberg. An inverse of the evalua-tion functional for typed -calculus. In Proceedings of the Sixth AnnualIEEE Symposium on Logic in Computer Science, pages 203{211, Ams-terdam, The Netherlands, July 1991. IEEE Computer Society Press.[6] Andrew A. Berlin. A compilation strategy for numerical programs basedon partial evaluation. Master's thesis, MIT Arti cial Intelligence Lab-oratory, July 1989. Technical report 1144.[7] Andrew A. Berlin. Partial evaluation applied to numerical computation.In Mitchell Wand, editor, Proceedings of the 1990 ACM Conference on27 Lisp and Functional Programming, pages 139{150, Nice, France, June1990. ACM Press.[8] Andrew A. Berlin and Daniel Weise. Compiling scienti c code usingpartial evaluation. IEEE Computer, 23(12):25{37, December 1990.[9] Anders Bondorf. Similix manual, system version 3.0. Technical Report91/9, DIKU, Computer Science Department, University of Copenhagen,Copenhagen, Denmark, 1991.[10] Anders Bondorf and Olivier Danvy. Automatic autoprojection of recur-sive equations with global variables and abstract data types. Scienceof Computer Programming, 16:151{195, 1991.[11] William Clinger and Jonathan Rees, editors. Revised4 report onthe algorithmic language Scheme. LISP Pointers, IV(3):1{55, July-September 1991.[12] Charles Consel. New insights into partial evaluation: the Schism ex-periment. In Harald Ganzinger, editor, Proceedings of the Second Eu-ropean Symposium on Programming, number 300 in Lecture Notes inComputer Science, pages 236{246, Nancy, France, March 1988.[13] Charles Consel and Olivier Danvy. Static and dynamic semantics pro-cessing. In Robert (Corky) Cartwright, editor, Proceedings of the Eigh-teenth Annual ACM Symposium on Principles of Programming Lan-guages, pages 14{24, Orlando, Florida, January 1991. ACM Press.[14] Charles Consel and Olivier Danvy. Tutorial notes on partial evaluation.In Susan L. Graham, editor, Proceedings of the Twentieth Annual ACMSymposium on Principles of Programming Languages, pages 493{501,Charleston, South Carolina, January 1993. ACM Press.[15] Catarina Coquand. From semantics to rules: A machine assisted anal-ysis. In Egon Borger, Yuri Gurevich, and Karl Meinke, editors, Pro-ceedings of CSL'93, number 832 in Lecture Notes in Computer Science.Springer-Verlag, 1993.[16] Djordje Cubri c, Peter Dybjer, and Philip Scott. Normalization andthe Yoneda embedding. Mathematical Structures in Computer Science,1997. To appear.28 [17] Olivier Danvy. Type-directed partial evaluation. In Guy L. Steele Jr.,editor, Proceedings of the Twenty-Third Annual ACM Symposium onPrinciples of Programming Languages, pages 242{257, St. PetersburgBeach, Florida, January 1996. ACM Press.[18] Olivier Danvy. Pragmatics of type-directed partial evaluation. Tech-nical Report BRICS RS-96-15, Department of Computer Science, Uni-versity of Aarhus, Aarhus, Denmark, May 1996.[19] Olivier Danvy and Andrzej Filinski. Representing control, a study ofthe CPS transformation.Mathematical Structures in Computer Science,2(4):361{391, December 1992.[20] Olivier Danvy, Karoline Malmkj r, and Jens Palsberg. The essenceof eta-expansion in partial evaluation. LISP and Symbolic Computa-tion, 8(3):209{227, 1995. An earlier version appeared in the proceed-ings of the 1994 ACM SIGPLAN Workshop on Partial Evaluation andSemantics-Based Program Manipulation.[21] Olivier Danvy and Morten Rhiger. Compiling actions by type-directedpartial evaluation. In Proceedings of the 9th Nordic Workshop on Pro-gramming Theory, Tallinn, Estonia, October 1997.[22] Olivier Danvy and Ren e Vestergaard. Semantics-based compiling: Acase study in type-directed partial evaluation. In Herbert Kuchen andDoaitse Swierstra, editors, Eighth International Symposium on Pro-gramming Language Implementation and Logic Programming, number1140 in Lecture Notes in Computer Science, pages 182{197, Aachen,Germany, September 1996. Springer-Verlag. Extended version avail-able as the technical report BRICS-RS-96-13.[23] Scott Draves. Automatic Program Specialization for Interactive Media.PhD thesis, School of Computer Science, Carnegie Mellon University,Pittsburgh, Pennsylvania, May 1997. Technical Report CMU-CS-97-159.[24] R. Kent Dybvig. The Scheme Programming Language. Prentice-Hall,1987.[25] Mayer Goldberg. Recursive Application Survival in the -Calculus. PhDthesis, Computer Science Department, Indiana University, Blooming-ton, Indiana, May 1996.29 [26] William L. Harrison and Samuel N. Kamin. Compilation as partialevaluation of functor category semantics. Technical report, Departmentof Computer Science, University of Illinois at Urbana-Champaign, Ur-bana, Illinois, 1997.[27] John Hughes. Type specialisation for the lambda calculus. In OlivierDanvy, Robert Gluck, and Peter Thiemann, editors, Partial Evalua-tion, number 1110 in Lecture Notes in Computer Science, Dagstuhl,Germany, February 1996. Springer-Verlag.[28] Neil D. Jones, Carsten K. Gomard, and Peter Sestoft. Partial Evalu-ation and Automatic Program Generation. Prentice Hall InternationalSeries in Computer Science. Prentice-Hall, 1993.[29] Eugene E. Kohlbecker. Syntactic Extensions in the Programming Lan-guage Lisp. PhD thesis, Indiana University, Bloomington, Indiana,1986.[30] Eugene E. Kohlbecker, Daniel P. Friedman, Matthias Felleisen, andBruce Duba. Hygienic macro expansion. In William L. Scherlis andJohn H. Williams, editors, Proceedings of the 1986 ACM Conferenceon Lisp and Functional Programming, pages 151{161, Cambridge, Mas-sachusetts, August 1986.[31] Torben . Mogensen. Partially static structures in a self-applicablepartial evaluator. In Dines Bj rner, Andrei P. Ershov, and Neil D.Jones, editors, Partial Evaluation and Mixed Computation, pages 325{347. North-Holland, 1988.[32] Peter D. Mosses. Action Semantics, volume 26 of Cambridge Tracts inTheoretical Computer Science. Cambridge University Press, 1992.[33] Frank Pfenning. Logic programming in the LF logical framework. InG erard Huet and Gordon Plotkin, editors, Logical Frameworks, pages149{181. Cambridge University Press, 1991.[34] Morten Rhiger. A study in higher-order programming languages. Mas-ter's thesis, DAIMI, Department of Computer Science, University ofAarhus, Aarhus, Denmark, December 1997.[35] Tim Sheard. A type-directed, on-line, partial evaluator for a poly-morphic language. In Charles Consel, editor, Proceedings of the ACM30 SIGPLAN Symposium on Partial Evaluation and Semantics-Based Pro-gram Manipulation, pages 22{35, Amsterdam, The Netherlands, June1997. ACM Press.[36] Ren e Vestergaard. From proof normalization to compiler generationand type-directed change-of-representation. Master's thesis, DAIMI,Department of Computer Science, University of Aarhus, Aarhus, Den-mark, May 1997.[37] Daniel Weise, Roland Conybeare, Erik Ruf, and Scott Seligman. Auto-matic online partial evaluation. In John Hughes, editor, Proceedings ofthe Fifth ACM Conference on Functional Programming and ComputerArchitecture, number 523 in Lecture Notes in Computer Science, pages165{191, Cambridge, Massachusetts, August 1991. Springer-Verlag.

برای دانلود رایگان متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Online Type-Directed Partial Evaluation for Dynamically-Typed Languages∗

This article presents an alternative method of type-directed partial evaluation, which is simpler and more efficient than previous methods. Unlike previous methods, it is straightforwardly applicable to functional languages with various powerful type systems. As an extreme instance, this article mainly deals with a dynamically-typed functional language like Scheme. The key idea is to extend pri...

متن کامل

Pragmatics of Type-Directed Partial Evaluation

Type directed partial evaluation stems from the residualiz ation of arbitrary static values in dynamic contexts given their type Its algorithm coincides with the one for coercing a subtype value into a supertype value which itself coincides with the one of normalization in the calculus Type directed partial evaluation is thus used to specialize compiled closed programs given their type Since Si...

متن کامل

Memoization in Type-Directed Partial Evaluation

We use a code generator—type-directed partial evaluation— to verify conversions between isomorphic types, or more precisely to verify that a composite function is the identity function at some complicated type. A typed functional language such as ML provides a natural support to express the functions and type-directed partial evaluation provides a convenient setting to obtain the normal form of...

متن کامل

Two Flavors of Offline Partial Evaluation

Type-directed partial evaluation is a new approach to program specialization for functional programming languages. Its merits with respect to the traditional offline partial evaluation approach have not yet been fully explored. We present a comparison of type-directed partial evaluation with standard offline partial evaluation in both a qualitative and quantitative way. For the latter we use im...

متن کامل

Two Flavors of Ooine Partial Evaluation

Type-directed partial evaluation is a new approach to program specialization for functional programming languages. Its merits with respect to the traditional ooine partial evaluation approach have not yet been fully explored. We present a comparison of type-directed partial evaluation with standard ooine partial evaluation in both a qualitative and quantitative way. For the latter we use implem...

متن کامل

Pragmatic Aspects of Type-Directed Partial Evaluation

Type-directed partial evaluation stems from the residualization of static values in dynamic contexts, given their type and the type of their free variables. Its algorithm coincides with the algorithm for coercing a subtype value into a supertype value, which itself coincides with Berger and Schwichtenberg’s normalization algorithm for the simply typed λ-calculus. Type-directed partial evaluatio...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 1998